# encoding: utf-8


"""

@author: tongzhenguo

@time: 2021/5/10 下午9:52

@desc:

主要是加仓,交易价格的分析
1.制定三段价格买入点,一个价格卖出点的交易计划

模版：{CODE}止损线{PRICE1}，压力位{PRICE2}；预期1手收益{EARINGS}
"""
from collections import Counter

from baostock_data import StockBase
from const import PROJ_HOME
from date_util import now_date_fmt


class PlanA(StockBase):
    """频繁出现的价格点，选择三个历时较久的"""

    def __init__(self, input, output, start_date=None, end_date=None):
        super().__init__()
        self.obj = StockBase()
        self.white_list = ["新和成", "鱼跃医疗", "丽珠集团", "中国平安", "歌尔股份", "顺丰控股", "韦尔股份"]
        self.start_date = start_date if start_date else "2020-01-01"
        self.end_date = end_date if end_date else now_date_fmt("YYYY-MM-DD")

    def run(self):
        white_list_stock_data = self.obj.stock_data[self.obj.stock_data["name"].isin(self.white_list)]
        target_price_list__ = []
        for row in white_list_stock_data[["name", "code"]].values:
            code = row[1]
            name = row[0]
            stock_data = self.obj.get_history_k_data(code, name, self.start_date, self.end_date)
            stock_data["close"] = stock_data["close"].apply(lambda _: int(float(_)))
            close_price_list = stock_data["close"].values
            counter = Counter(close_price_list)
            import_price_list = counter.most_common(10)
            import_price_list = sorted(import_price_list, key=lambda kv: kv[0])
            total_cnt = sum([v for k, v in import_price_list])
            cnt = 0
            target_price_list = [import_price_list[0][0]]
            for k, v in import_price_list:
                if cnt < total_cnt / 3 <= cnt + v:
                    target_price_list.append(k)
                elif cnt < 2 * total_cnt / 3 <= cnt + v and k not in target_price_list:
                    target_price_list.append(k)
                cnt += v
            target_price_list__.append("-".join(map(str, target_price_list)))
            # print(name, target_price_list)
        white_list_stock_data["目标价"] = target_price_list__
        print(white_list_stock_data[["name", "目标价"]])
        white_list_stock_data.to_csv(self.output, index=False)


class PlanB(StockBase):
    """趋势线/撑压线/价格形态"""

    def __init__(self, input, output, start_date=None, end_date=None):
        super().__init__()
        self.obj = StockBase()
        self.white_list = ["新和成", "鱼跃医疗", "丽珠集团", "中国平安", "三一重工"]
        self.start_date = start_date if start_date else "2020-01-01"
        self.end_date = end_date if end_date else now_date_fmt("YYYY-MM-DD")

    def run(self):
        white_list_stock_data = self.obj.stock_data[self.obj.stock_data["name"].isin(self.white_list)]
        target_price_list__ = []
        for row in white_list_stock_data[["name", "code"]].values:
            code = row[1]
            name = row[0]
            stock_data = self.obj.get_history_k_data(code, name, self.start_date, self.end_date)
            stock_data["close"] = stock_data["close"].apply(lambda _: int(float(_)))
            close_price_list = stock_data["close"].values
            counter = Counter(close_price_list)
            import_price_list = counter.most_common(10)
            import_price_list = sorted(import_price_list, key=lambda kv: kv[0])
            total_cnt = sum([v for k, v in import_price_list])
            cnt = 0
            target_price_list = [import_price_list[0][0]]
            for k, v in import_price_list:
                if cnt < total_cnt / 3 <= cnt + v:
                    target_price_list.append(k)
                elif cnt < 2 * total_cnt / 3 <= cnt + v and k not in target_price_list:
                    target_price_list.append(k)
                cnt += v
            target_price_list__.append("-".join(map(str, target_price_list)))
            # print(name, target_price_list)
        white_list_stock_data["目标价"] = target_price_list__
        white_list_stock_data.to_csv(self.output, index=False)


if __name__ == "__main__":
    input_path = PROJ_HOME + "/data/baostock"
    output_path = PROJ_HOME + "/data/baostock_with_target_price"
    pa = PlanA(input_path, output_path)
    pa.run()
